package com.achilles.learncontentprovider;

import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Uri uriBook = BookProvider.BOOK_CONTENT_URI;

        readBookInfo(uriBook);
        insertBookInfo(uriBook);
        readBookInfo(uriBook);
    }

    private void readBookInfo(Uri uri) {
        Cursor bookCursor = getContentResolver().query(uri,null,null,null,null);
        while (bookCursor.moveToNext()) {
            Book book = new Book();
            book.id = bookCursor.getInt(0);
            book.name = bookCursor.getString(1);
            Log.d("AchillesL",book.toString());
        }
        bookCursor.close();
    }

    private void insertBookInfo(Uri uri) {
        ContentValues values = new ContentValues();
        values.put("_id",6);
        values.put("name","编译原理");
        getContentResolver().insert(uri,values);
    }
}
